﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Kaifa.SharePoint.RucCommon.DBAccess;

namespace VenueManage
{


    public class RucOrganizationExtend : IEqualityComparer<RucOrganizationExtend>
    {
        private Kaifa.SharePoint.RucCommon.DBAccess.RUC_Organization rucorganization;
        public Kaifa.SharePoint.RucCommon.DBAccess.RUC_Organization Organization
        {
            get { return rucorganization; }
            set { rucorganization = value; }
        }

        /// <summary>
        /// Ruc_Users表的ID
        /// </summary>
        private int rucuserid;
        public int RucUserID
        {
            get { return rucuserid; }
            set { rucuserid = value; }
        }

        /// <summary>
        /// 人员维度ID
        /// </summary>
        private int userdimensionid;
        public int UserDimensionID
        {
            get { return userdimensionid; }
            set { userdimensionid = value; }
        }

        public bool Equals(RucOrganizationExtend x, RucOrganizationExtend y)
        {
            return x.Organization.ID.Equals(y.Organization.ID);
        }
        public int GetHashCode(RucOrganizationExtend obj)
        {
            return 0;
        }
    }


    public class RUC
    {
        

        public List<RucOrganizationExtend> GetOrgLayeringBySamAccountNames(string SamAccountName)
        {
            List<RucOrganizationExtend> lstExtend = new List<RucOrganizationExtend>();
            List<RucOrganizationExtend> lstTmp = new List<RucOrganizationExtend>();
            try
            {
                RucDA rda = new RucDA();
                int ThirdID = 0;
                List<RUC_Users> users = new RucDA().GetUsersByAdAccount(SamAccountName, false);
                foreach (RUC_Users user in users)
                {
                    RUC_Organization orgFourth = new RUC_Organization();
                    RucOrganizationExtend ExtendFourth = new RucOrganizationExtend();
                    List<string> lstValue = new List<string>() { user.Orge2, user.Orge3, user.Orge4, user.Orge5, user.Orge6, user.Orge7, user.Orge8 };
                    int lastID = 0;
                    bool thisDelete = false;
                    for (int i = 0; i < lstValue.Count; i++)
                    {
                        if (lstValue[i] == "00000000" || lstValue[i] == string.Empty)
                            break;
                        List<RUC_Organization> org = rda.GetOrgByValue(lstValue[i], false);
                        if (org.Count > 0)
                        {
                            if (i == 0 && org[0].ParentID != null)
                            {
                                //LogHelper.Error(MethodBase.GetCurrentMethod().DeclaringType, "RUC_Users表ID为" + user.ID + "的Orge2组织不能有ParentID！");
                                break;
                            }
                            RucOrganizationExtend Extend = new RucOrganizationExtend();
                            Extend.Organization = org[0];
                            Extend.UserDimensionID = user.DimensionID ?? 1;
                            Extend.RucUserID = user.ID;
                            lstTmp.Add(Extend);
                            if (org[0].OrgTypeID == 1)
                                lstExtend.Add(Extend);
                            else if (org[0].OrgTypeID == 2)
                            {
                                if (Extend.Organization.ParentID != lastID)
                                {
                                    //LogHelper.Fatal(MethodBase.GetCurrentMethod().DeclaringType, "人员ID：" + user.ID + "、组织ID为：" + lstValue[i] + "的上级组织与ID:" + lastID + "不匹配！");
                                    thisDelete = true;
                                    break;
                                }
                                lstExtend.Add(Extend);
                            }
                            else if (org[0].OrgTypeID == 3)
                            {
                                RUC_Organization orgTmp = rda.GetOrganizationByID(org[0].ParentID ?? 0);
                                if (orgTmp.OrgTypeID == 2 && lstExtend.Where(p => p.Organization.ID == org[0].ID).Count() == 0)
                                {
                                    if (orgTmp.Value.Trim() == string.Empty && orgTmp.ParentID != lastID)    //orgTmp是虚拟组织时，判断上级的上级是否匹配
                                    {
                                        //LogHelper.Fatal(MethodBase.GetCurrentMethod().DeclaringType, "人员ID：" + user.ID + "、组织ID为：" + lstValue[i] + "的上级组织与ID:" + lastID + "不匹配！");
                                        thisDelete = true;
                                        break;
                                    }
                                    RucOrganizationExtend ExtendTmp = new RucOrganizationExtend();
                                    ExtendTmp.Organization = orgTmp;
                                    ExtendTmp.UserDimensionID = user.DimensionID ?? 1;
                                    ExtendTmp.RucUserID = user.ID;
                                    lstExtend.Add(ExtendTmp);
                                    lstTmp.Add(ExtendTmp);
                                }
                                lstExtend.Add(Extend);
                                ThirdID = org[0].ID;
                            }
                            else if (org[0].OrgTypeID == 4)
                            {
                                if (Extend.Organization.ParentID != lastID)
                                {
                                    //LogHelper.Fatal(MethodBase.GetCurrentMethod().DeclaringType, "人员ID：" + user.ID + "、组织ID为：" + lstValue[i] + "的上级组织与ID:" + lastID + "不匹配！");
                                    thisDelete = true;
                                    break;
                                }
                                orgFourth.ID = org[0].ID;
                                orgFourth.ParentID = ThirdID;
                                orgFourth.ABBR += org[0].ABBR + "\\";
                                orgFourth.CName += org[0].CName + "\\";
                                orgFourth.EName += org[0].EName + "\\";
                                orgFourth.Value += org[0].Value + "\\";
                                orgFourth.OrgTypeID = 4;
                                orgFourth.IfDelete = org[0].IfDelete;
                                orgFourth.DimensionID = org[0].DimensionID;
                            }
                            lastID = Extend.Organization.ID;
                        }
                        else
                        {
                            //LogHelper.Fatal(MethodBase.GetCurrentMethod().DeclaringType, "人员ID：" + user.ID + "找不到组织ID为：" + lstValue[i] + "的组织！");
                            thisDelete = true;
                            break;
                        }
                    }
                    if (thisDelete)
                    {
                        foreach (RucOrganizationExtend rucOrganization in lstTmp)
                        {
                            lstExtend.Remove(rucOrganization);
                        }
                        lstTmp.Clear();
                    }
                    if (lstTmp.Count > 0 && orgFourth.ABBR != null && orgFourth.CName != null && orgFourth.EName != null && orgFourth.Value != null)
                    {
                        orgFourth.ABBR = orgFourth.ABBR.Trim('\\');
                        orgFourth.CName = orgFourth.CName.Trim('\\');
                        orgFourth.EName = orgFourth.EName.Trim('\\');
                        orgFourth.Value = orgFourth.Value.Trim('\\');
                        ExtendFourth.Organization = orgFourth;
                        ExtendFourth.UserDimensionID = user.DimensionID ?? 1;
                        lstExtend.Add(ExtendFourth);
                        ThirdID = 0;
                        lstTmp.Clear();
                    }
                }
            }
            catch (Exception ex)
            {
                VBBLL.Worker.writeErrorMessage("0", ex, "RUC");
            }

            return lstExtend.Distinct(new RucOrganizationExtend()).ToList();
        }



    }
}